import os
from environment_pool import *
from interface import * 
#配置环境参数
Environment = {
    "B" : 1.16e-9,
    "pS" : 563,
    "R1" : 1000,
    "g" : 0.002,
    "L2" : 3e9,
    ## preset of the population
    "popsize" : 5,    # population size.
    "relation_matrix_col":3,#干扰关联矩阵的列数即接收天线的个数
    "npar" : 2,		# Dimension of the problem.  每个接收天线的位置由2个参数控制，所以此处的参数总数为2*relation_matrix_col
    "param_maxit" :3,		# Maximum number of iterations.
    "deltax" : 0.0001, #calculation accuracy of difference method
    "target_couple": [61.2,40.8,86.2] #控制的隔离度参数，最大值达到该值后退出迭代，此处的数据长度为relation_matrix_col，每个收发对给一个隔离度控制参数，单位为dB
}

medo_interface0 = medo_interface(Environment)
lb = -np.ones((Environment["npar"])) * 5.12 #Lower dimension boundary. #按照[u1,v1,u2,v2,...,un,vn]，其中n为接收机个数
ub = np.ones((Environment["npar"])) * 5.12 #Upper dimension boundary.
medo_interface0.set_boundary(lb,ub)

mesh_file = r''
from utils import Geodesic_cfg_and_run,mesh_filter_sphere,tri6_interp
creeping_data = {
    "Rx":np.array([   #设置发射天线位置附近的曲面三角形（六点）坐标，按照1->2->3->12中点->23中点->13中点顺序排列，接收天线位置在区域内进行优化调整
        [9012.79279, 0,  4364.822826],   #1  
        [9002.79279, 10,  4364.822826],   #2
        [9002.79279, 0,  4374.822826],    #3
        [9007.79279, 5,  4364.822826],  #12中点
        [9002.79279, 5,  4369.822826],   #23中点
        [9007.79279, 0,  4369.822826],     #13中点
    ]),
    "Tx":np.array([[6000, 0,  4484.85302],   #N个接收机的位置点（固定不变，不用优化），一行上对应的N个接收机
                   [8234.5, 555.7,  4156.24],   
                   [8498.79, 861.36,  2878.88],   
    ]),
    "mesh_file" : "plane.nas", #"A320-mesh_new.facet", #网格文件名（要默认放当前文件夹下） #A320-mesh2.nas
    "results_filename" : "results", #结果文件名（可默认）
    "freq" : 1.268e9,   #隔离度计算频率（Hz)
    # "Rx_r" : 1.2,   #接收天线的网格筛选半径
}
#设置网格计算的参数边界（参数：点序号）
lb[:] = 0
ub[:] = 1
medo_interface0.set_boundary(lb,ub)

path=r'E:/BHA_PUBLISH333/Geodesic'
#path = template.WorkPath
medo_interface0.build_benchmark(path,creeping_data)
from MEDO_EN import MEDO_solver


xminout,fxmin_global = MEDO_solver()
np.savetxt('results.txt',-fxmin_global,fmt='%.12f')
print('fxmin_global',fxmin_global)
best_ans = -np.min(fxmin_global)  #print the optimal solution
print('xminout',xminout)
uminout = xminout[0];vminout = xminout[1]
ptsminout = tri6_interp(uminout,vminout)
print('ptsminout',ptsminout)